from functools import lru_cache

@lru_cache(maxsize=1024)
def edit_distance_python(s1: str, s2: str) -> int:
    """
    经典的 Levenshtein 编辑距离算法实现。
    使用 LRU 缓存来加速对相同单词对的重复计算。
    """
    if s1 is None: s1 = ""
    if s2 is None: s2 = ""

    m, n = len(s1), len(s2)
    
    # 初始化DP矩阵
    dp = [[0] * (n + 1) for _ in range(m + 1)]
    for i in range(m + 1):
        dp[i][0] = i
    for j in range(n + 1):
        dp[0][j] = j

    # 填充DP矩阵
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            cost = 0 if s1[i - 1] == s2[j - 1] else 1
            dp[i][j] = min(dp[i - 1][j] + 1,        # 删除
                           dp[i][j - 1] + 1,        # 插入
                           dp[i - 1][j - 1] + cost) # 替换

    return dp[m][n] 